using System.Collections;
using System.Collections.Generic;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestFMDefClassFactory
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        [Test]
        public void Test_CreateFactory()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(IDefClassFactory), defClassFactory);
        }

        [Test]
        public void Test_CreateUIFormFieldDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UIControlType uiControlType = TestUtilsUIControlType.CreateUnsavedValidUIControlType();
            UIControlMapperType uiControlMapperType = TestUtilsUIControlMapperType.CreateUnsavedValidUIControlMapperType();
            solution.UIControlMapperTypes.Add(uiControlMapperType);
            solution.UIControlTypes.Add(uiControlType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            const string label = "label";
            const string propertyname = "propertyName";
            string controlTypeName = uiControlType.TypeName;
            string controlAssembly = uiControlType.AssemblyName;
            string mapperTypeName = uiControlMapperType.TypeName;
            string mapperAssembly = uiControlMapperType.AssemblyName;
            const bool editable = true;
            const string toolTipText = "tool tip text";
            Hashtable attributes = new Hashtable { { "key", "value" } };
            const LayoutStyle layoutStyle = LayoutStyle.GroupBox;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            IUIFormField uiFormField = defClassFactory.CreateUIFormProperty(label, propertyname, controlTypeName, controlAssembly,
                                                                            mapperTypeName, mapperAssembly, editable, false, toolTipText, attributes, layoutStyle);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIField), uiFormField);
            Assert.AreEqual(label, uiFormField.Label);
            Assert.AreEqual(propertyname, uiFormField.PropertyName);
            Assert.AreEqual(controlTypeName, uiFormField.ControlTypeName);
            Assert.AreEqual(controlAssembly, uiFormField.ControlAssemblyName);

            Assert.AreEqual(mapperTypeName, uiFormField.MapperTypeName);
            Assert.AreEqual(mapperAssembly, uiFormField.MapperAssembly);

            Assert.AreEqual(editable, uiFormField.Editable);
            Assert.AreEqual(toolTipText, uiFormField.ToolTipText);
            Assert.AreEqual(attributes.Count, uiFormField.Parameters.Count);
            Assert.AreEqual(attributes["key"], uiFormField.Parameters["key"]);
            Assert.AreEqual(layoutStyle, uiFormField.Layout);
        }

        [Test]
        public void Test_CreateUIFormFieldDef_WithCustomUnknownControlType()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UIControlMapperType uiControlMapperType = TestUtilsUIControlMapperType.CreateUnsavedValidUIControlMapperType();
            solution.UIControlMapperTypes.Add(uiControlMapperType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            const string label = "label";
            const string propertyname = "propertyName";
            string controlTypeName = TestUtilsShared.GetRandomString();
            string controlAssembly = TestUtilsShared.GetRandomString();
            string mapperTypeName = uiControlMapperType.TypeName;
            string mapperAssembly = uiControlMapperType.AssemblyName;
            const bool editable = true;
            const string toolTipText = "tool tip text";
            Hashtable attributes = new Hashtable { { "key", "value" } };
            const LayoutStyle layoutStyle = LayoutStyle.GroupBox;
            //---------------Assert Precondition----------------
            Assert.IsNull(solution.GetUIFormFieldControlType(controlAssembly, controlTypeName));
            //---------------Execute Test ----------------------

            IUIFormField uiFormField = defClassFactory.CreateUIFormProperty(label, propertyname, controlTypeName, controlAssembly,
                                                                            mapperTypeName, mapperAssembly, editable, false, toolTipText, attributes, layoutStyle);
            //---------------Test Result -----------------------
            Assert.IsNotNull(solution.GetUIFormFieldControlType(controlAssembly, controlTypeName));
            Assert.IsInstanceOf(typeof(UIField), uiFormField);
            Assert.AreEqual(label, uiFormField.Label);
            Assert.AreEqual(propertyname, uiFormField.PropertyName);
            Assert.AreEqual(controlTypeName, uiFormField.ControlTypeName);
            Assert.AreEqual(controlAssembly, uiFormField.ControlAssemblyName);

            Assert.AreEqual(mapperTypeName, uiFormField.MapperTypeName);
            Assert.AreEqual(mapperAssembly, uiFormField.MapperAssembly);

            Assert.AreEqual(editable, uiFormField.Editable);
            Assert.AreEqual(toolTipText, uiFormField.ToolTipText);
            Assert.AreEqual(attributes.Count, uiFormField.Parameters.Count);
            Assert.AreEqual(attributes["key"], uiFormField.Parameters["key"]);
            Assert.AreEqual(layoutStyle, uiFormField.Layout);
        }

        [Test]
        public void Test_CreateUIFormFieldDef_WithBlankControlType_ShouldHaveNullColumnType()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UIControlMapperType uiControlMapperType = TestUtilsUIControlMapperType.CreateUnsavedValidUIControlMapperType();
            solution.UIControlMapperTypes.Add(uiControlMapperType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            const string label = "label";
            const string propertyname = "propertyName";
            string controlTypeName = string.Empty;
            string controlAssembly = string.Empty;
            string mapperTypeName = uiControlMapperType.TypeName;
            string mapperAssembly = uiControlMapperType.AssemblyName;
            const bool editable = true;
            const string toolTipText = "tool tip text";
            Hashtable attributes = new Hashtable { { "key", "value" } };
            const LayoutStyle layoutStyle = LayoutStyle.GroupBox;
            //---------------Assert Precondition----------------
            Assert.IsNull(solution.GetUIFormFieldControlType(controlAssembly, controlTypeName));
            //---------------Execute Test ----------------------

            IUIFormField uiFormField = defClassFactory.CreateUIFormProperty(label, propertyname, controlTypeName, controlAssembly,
                                                                            mapperTypeName, mapperAssembly, editable, false, toolTipText, attributes, layoutStyle);
            //---------------Test Result -----------------------
            Assert.IsNull(solution.GetUIFormFieldControlType(controlAssembly, controlTypeName));
            Assert.IsInstanceOf(typeof(UIField), uiFormField);
            Assert.AreEqual(label, uiFormField.Label);
            Assert.AreEqual(propertyname, uiFormField.PropertyName);
            Assert.AreEqual(controlTypeName, uiFormField.ControlTypeName);
            Assert.AreEqual(controlAssembly, uiFormField.ControlAssemblyName);
            Assert.IsNull(((UIField )uiFormField).UIControlType);

            Assert.AreEqual(mapperTypeName, uiFormField.MapperTypeName);
            Assert.AreEqual(mapperAssembly, uiFormField.MapperAssembly);

            Assert.AreEqual(editable, uiFormField.Editable);
            Assert.AreEqual(toolTipText, uiFormField.ToolTipText);
            Assert.AreEqual(attributes.Count, uiFormField.Parameters.Count);
            Assert.AreEqual(attributes["key"], uiFormField.Parameters["key"]);
            Assert.AreEqual(layoutStyle, uiFormField.Layout);
        }

        [Test]
        public void Test_CreateUIFormFieldDef_WithBlankControlAssembly()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UIControlType uiControlType = TestUtilsUIControlType.CreateUnsavedValidUIControlType();
            UIControlMapperType uiControlMapperType = TestUtilsUIControlMapperType.CreateUnsavedValidUIControlMapperType();
            solution.UIControlMapperTypes.Add(uiControlMapperType);
            solution.UIControlTypes.Add(uiControlType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            const string label = "label";
            const string propertyname = "propertyName";
            string controlTypeName = uiControlType.TypeName;
            string controlAssembly = string.Empty;
            string mapperTypeName = uiControlMapperType.TypeName;
            string mapperAssembly = uiControlMapperType.AssemblyName;
            const bool editable = true;
            const string toolTipText = "tool tip text";
            Hashtable attributes = new Hashtable { { "key", "value" } };
            const LayoutStyle layoutStyle = LayoutStyle.GroupBox;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            IUIFormField uiFormField = defClassFactory.CreateUIFormProperty(label, propertyname, controlTypeName, controlAssembly,
                                                                            mapperTypeName, mapperAssembly, editable, false, toolTipText, attributes, layoutStyle);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIField), uiFormField);
            Assert.AreEqual(label, uiFormField.Label);
            Assert.AreEqual(propertyname, uiFormField.PropertyName);
            Assert.AreEqual(controlTypeName, uiFormField.ControlTypeName);
            Assert.AreEqual(uiControlType.AssemblyName, uiFormField.ControlAssemblyName);
            Assert.AreSame(uiControlType, ((UIField)uiFormField).UIControlType);

            Assert.AreEqual(mapperTypeName, uiFormField.MapperTypeName);
            Assert.AreEqual(mapperAssembly, uiFormField.MapperAssembly);

            Assert.AreEqual(editable, uiFormField.Editable);
            Assert.AreEqual(toolTipText, uiFormField.ToolTipText);
            Assert.AreEqual(attributes.Count, uiFormField.Parameters.Count);
            Assert.AreEqual(attributes["key"], uiFormField.Parameters["key"]);
            Assert.AreEqual(layoutStyle, uiFormField.Layout);
        }

        [Test]
        public void Test_CreateUIFormFieldDef_WithCustomUnknownControlMapperType()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UIControlType uiControlType = TestUtilsUIControlType.CreateUnsavedValidUIControlType();
            solution.UIControlTypes.Add(uiControlType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            const string label = "label";
            const string propertyname = "propertyName";
            string controlTypeName = uiControlType.TypeName;
            string controlAssembly = uiControlType.AssemblyName;
            string mapperTypeName = TestUtilsShared.GetRandomString();
            string mapperAssembly = TestUtilsShared.GetRandomString();
            const bool editable = true;
            const string toolTipText = "tool tip text";
            Hashtable attributes = new Hashtable { { "key", "value" } };
            const LayoutStyle layoutStyle = LayoutStyle.GroupBox;
            //---------------Assert Precondition----------------
            Assert.IsNull(solution.GetUIControlMapperType(mapperAssembly, mapperTypeName));
            //---------------Execute Test ----------------------

            IUIFormField uiFormField = defClassFactory.CreateUIFormProperty(label, propertyname, controlTypeName, controlAssembly,
                                                                            mapperTypeName, mapperAssembly, editable, false, toolTipText, attributes, layoutStyle);
            //---------------Test Result -----------------------
            Assert.IsNotNull(solution.GetUIControlMapperType(mapperAssembly, mapperTypeName));
            Assert.IsInstanceOf(typeof(UIField), uiFormField);
            Assert.AreEqual(label, uiFormField.Label);
            Assert.AreEqual(propertyname, uiFormField.PropertyName);
            Assert.AreEqual(controlTypeName, uiFormField.ControlTypeName);
            Assert.AreEqual(controlAssembly, uiFormField.ControlAssemblyName);

            Assert.AreEqual(mapperTypeName, uiFormField.MapperTypeName);
            Assert.AreEqual(mapperAssembly, uiFormField.MapperAssembly);

            Assert.AreEqual(editable, uiFormField.Editable);
            Assert.AreEqual(toolTipText, uiFormField.ToolTipText);
            Assert.AreEqual(attributes.Count, uiFormField.Parameters.Count);
            Assert.AreEqual(attributes["key"], uiFormField.Parameters["key"]);
            Assert.AreEqual(layoutStyle, uiFormField.Layout);
        }

        [Test]
        public void Test_CreateUIFormFieldDef_WithBlankControlMapperType_ShouldHaveBlankControlMapperType()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UIControlType uiControlType = TestUtilsUIControlType.CreateUnsavedValidUIControlType();
            solution.UIControlTypes.Add(uiControlType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            const string label = "label";
            const string propertyname = "propertyName";
            string controlTypeName = uiControlType.TypeName;
            string controlAssembly = uiControlType.AssemblyName;
            string mapperTypeName = string.Empty;
            string mapperAssembly = string.Empty;
            const bool editable = true;
            const string toolTipText = "tool tip text";
            Hashtable attributes = new Hashtable { { "key", "value" } };
            const LayoutStyle layoutStyle = LayoutStyle.GroupBox;
            //---------------Assert Precondition----------------
            Assert.IsNull(solution.GetUIControlMapperType(mapperAssembly, mapperTypeName));
            //---------------Execute Test ----------------------

            IUIFormField uiFormField = defClassFactory.CreateUIFormProperty(label, propertyname, controlTypeName, controlAssembly,
                                                                            mapperTypeName, mapperAssembly, editable, false, toolTipText, attributes, layoutStyle);
            //---------------Test Result -----------------------
            Assert.IsNull(solution.GetUIControlMapperType(mapperAssembly, mapperTypeName));
            Assert.IsInstanceOf(typeof(UIField), uiFormField);
            Assert.AreEqual(label, uiFormField.Label);
            Assert.AreEqual(propertyname, uiFormField.PropertyName);
            Assert.AreEqual(controlTypeName, uiFormField.ControlTypeName);
            Assert.AreEqual(controlAssembly, uiFormField.ControlAssemblyName);

            Assert.AreEqual(mapperTypeName, uiFormField.MapperTypeName);
            Assert.AreEqual(mapperAssembly, uiFormField.MapperAssembly);
            Assert.IsNull(((UIField)uiFormField).UIControlMapperType);

            Assert.AreEqual(editable, uiFormField.Editable);
            Assert.AreEqual(toolTipText, uiFormField.ToolTipText);
            Assert.AreEqual(attributes.Count, uiFormField.Parameters.Count);
            Assert.AreEqual(attributes["key"], uiFormField.Parameters["key"]);
            Assert.AreEqual(layoutStyle, uiFormField.Layout);
        }

        [Test]
        public void Test_CreateUIFormFieldDef_WithBlankControlMapperAssembly()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UIControlType uiControlType = TestUtilsUIControlType.CreateUnsavedValidUIControlType();
            UIControlMapperType uiControlMapperType = TestUtilsUIControlMapperType.CreateUnsavedValidUIControlMapperType();
            solution.UIControlMapperTypes.Add(uiControlMapperType);
            solution.UIControlTypes.Add(uiControlType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            const string label = "label";
            const string propertyname = "propertyName";
            string controlTypeName = uiControlType.TypeName;
            string controlAssembly = uiControlType.AssemblyName;
            string mapperTypeName = uiControlMapperType.TypeName;
            string mapperAssembly = string.Empty;
            const bool editable = true;
            const string toolTipText = "tool tip text";
            Hashtable attributes = new Hashtable { { "key", "value" } };
            const LayoutStyle layoutStyle = LayoutStyle.GroupBox;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            IUIFormField uiFormField = defClassFactory.CreateUIFormProperty(label, propertyname, controlTypeName, controlAssembly,
                                                                            mapperTypeName, mapperAssembly, editable, false, toolTipText, attributes, layoutStyle);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIField), uiFormField);
            Assert.AreEqual(label, uiFormField.Label);
            Assert.AreEqual(propertyname, uiFormField.PropertyName);
            Assert.AreEqual(controlTypeName, uiFormField.ControlTypeName);
            Assert.AreEqual(controlAssembly, uiFormField.ControlAssemblyName);

            Assert.AreEqual(mapperTypeName, uiFormField.MapperTypeName);
            Assert.AreEqual(uiControlMapperType.AssemblyName, uiFormField.MapperAssembly);
            Assert.AreSame(uiControlMapperType, ((UIField)uiFormField).UIControlMapperType);

            Assert.AreEqual(editable, uiFormField.Editable);
            Assert.AreEqual(toolTipText, uiFormField.ToolTipText);
            Assert.AreEqual(attributes.Count, uiFormField.Parameters.Count);
            Assert.AreEqual(attributes["key"], uiFormField.Parameters["key"]);
            Assert.AreEqual(layoutStyle, uiFormField.Layout);
        }

        [Test]
        public void Test_CreatePropDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            DMPropertyType propertyType = new DMPropertyType(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString());
            solution.PropertyTypes.Add(propertyType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);

            string propertyName = TestUtilsShared.GetRandomString();
            const PropReadWriteRule readWriteRule = PropReadWriteRule.ReadWrite;
            string fieldName = TestUtilsShared.GetRandomString();
            string defaultValueString = TestUtilsShared.GetRandomString();
            int length = TestUtilsShared.GetRandomInt(3, 6);
            string displayName = TestUtilsShared.GetRandomString();
            string description = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IPropDef propDef = defClassFactory.CreatePropDef(propertyName, propertyType.AssemblyName,
                                                             propertyType.PropertyTypeName, readWriteRule,
                                                             fieldName, defaultValueString, true,
                                                             true, length, displayName,
                                                             description, true);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMProperty), propDef);
            Assert.AreEqual(propertyName, propDef.PropertyName);
            Assert.AreEqual(propertyType.AssemblyName, propDef.PropertyTypeAssemblyName);
            Assert.AreEqual(propertyType.PropertyTypeName, propDef.PropertyTypeName);
            Assert.AreEqual(readWriteRule, propDef.ReadWriteRule);
            Assert.AreEqual(fieldName, propDef.DatabaseFieldName);
            Assert.AreEqual(defaultValueString, propDef.DefaultValueString);
            Assert.IsTrue(propDef.Compulsory);
            Assert.IsTrue(propDef.AutoIncrementing);
            Assert.AreEqual(length, propDef.Length);
            Assert.AreEqual(displayName, propDef.DisplayName);
            Assert.AreEqual(description, propDef.Description);
            Assert.IsTrue(propDef.KeepValuePrivate);
        }

        [Test]
        public void Test_CreatePropDef_WithUnknownCustomPropertyType()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);

            string propertyName = TestUtilsShared.GetRandomString();
            string propertyTypeName = TestUtilsShared.GetRandomString();
            string propertyTypeAssemblyName = TestUtilsShared.GetRandomString();
            const PropReadWriteRule readWriteRule = PropReadWriteRule.ReadWrite;
            string fieldName = TestUtilsShared.GetRandomString();
            string defaultValueString = TestUtilsShared.GetRandomString();
            int length = TestUtilsShared.GetRandomInt(3, 6);
            string displayName = TestUtilsShared.GetRandomString();
            string description = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            Assert.IsNull(solution.GetDMPropertyType(propertyTypeAssemblyName, propertyTypeName));
            //---------------Execute Test ----------------------
            IPropDef propDef = defClassFactory.CreatePropDef(propertyName, propertyTypeAssemblyName,
                                                             propertyTypeName, readWriteRule,
                                                             fieldName, defaultValueString, true,
                                                             true, length, displayName,
                                                             description, true);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMProperty), propDef);
            Assert.AreEqual(propertyName, propDef.PropertyName);
            Assert.IsNotNull(solution.GetDMPropertyType(propertyTypeAssemblyName, propertyTypeName));
            Assert.AreEqual(propertyTypeAssemblyName, propDef.PropertyTypeAssemblyName);
            Assert.AreEqual(propertyTypeName, propDef.PropertyTypeName);
            Assert.AreEqual(readWriteRule, propDef.ReadWriteRule);
            Assert.AreEqual(fieldName, propDef.DatabaseFieldName);
            Assert.AreEqual(defaultValueString, propDef.DefaultValueString);
            Assert.IsTrue(propDef.Compulsory);
            Assert.IsTrue(propDef.AutoIncrementing);
            Assert.AreEqual(length, propDef.Length);
            Assert.AreEqual(displayName, propDef.DisplayName);
            Assert.AreEqual(description, propDef.Description);
            Assert.IsTrue(propDef.KeepValuePrivate);
        }

        [Test]
        public void Test_CreateKeyDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string keyName = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IKeyDef keyDef = defClassFactory.CreateKeyDef(keyName);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMUniqueConstraint), keyDef);
            Assert.AreEqual(keyName, keyDef.KeyName);
        }

        [Test]
        public void Test_CreatePrimaryKeyDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IPrimaryKeyDef primaryKeyDef = defClassFactory.CreatePrimaryKeyDef();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMObjectIdentity), primaryKeyDef);
        }

        [Test]
        public void Test_CreateBusinessObjectLookupList()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            DMClass dmClass = new DMClass();
            solution.DefaultAssembly.Classes.Add(dmClass);
            DMPropertyType propertyType = new DMPropertyType(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString());
            solution.PropertyTypes.Add(propertyType);
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string criteria = TestUtilsShared.GetRandomString();
            string sort = TestUtilsShared.GetRandomString();
            int timeout = TestUtilsShared.GetRandomInt();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ILookupList lookupList = defClassFactory.CreateBusinessObjectLookupList(
                solution.DefaultAssembly.AssemblyName, dmClass.ClassNameBO, criteria, sort, timeout);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMLookupListBusinessObject), lookupList);
            DMLookupListBusinessObject boLookupList = (DMLookupListBusinessObject) lookupList;
            Assert.AreEqual(criteria, boLookupList.Criteria);
            Assert.AreEqual(sort, boLookupList.SortString);
            Assert.AreEqual(timeout, boLookupList.Timeout);
            Assert.AreEqual(dmClass.ClassNameBO, boLookupList.ClassName);
            Assert.AreEqual(dmClass.Assembly.AssemblyName, boLookupList.AssemblyName);
        }
        
        //TODO Peter 31 Jul 2009: need to add processing to link bo lookup lists with correct dmclass once all clases are loaded
        
        [Test]
        public void Test_CreateSimpleLookupList()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            Dictionary<string, string> displayDictionary = new Dictionary<string, string> {{"1", TestUtilsShared.GetRandomString()}, {"2", TestUtilsShared.GetRandomString()}};
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ILookupList lookupList = defClassFactory.CreateSimpleLookupList(displayDictionary);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMLookupListSimple), lookupList);
            Assert.AreEqual(displayDictionary.Count, lookupList.GetLookupList().Count);
        }

        [Test]
        public void Test_CreateDatabaseLookupList()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string sqlString = TestUtilsShared.GetRandomString();
            int timeout = TestUtilsShared.GetRandomInt();
            string assemblyName = TestUtilsShared.GetRandomString();
            string className = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ILookupList lookupList = defClassFactory.CreateDatabaseLookupList(sqlString, timeout, assemblyName, className, true);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMLookupListDatabase), lookupList);
            DMLookupListDatabase lookupListDatabase = (DMLookupListDatabase) lookupList;
            Assert.AreEqual(sqlString, lookupListDatabase.Sql);
            Assert.AreEqual(timeout, lookupListDatabase.TimeOut);
            Assert.AreEqual(assemblyName, lookupListDatabase.Assembly);
            Assert.AreEqual(className, lookupListDatabase.LookupClassName);
            Assert.IsTrue(lookupListDatabase.LimitToList.Value);
        }

        private static void AssertPropRuleCreatedCorrectly(string name, string message, IPropRule propRule, string propRuleType)
        {
            Assert.IsInstanceOf(typeof(DMPropRule), propRule);
            DMPropRule dmPropRule = (DMPropRule) propRule;
            Assert.AreEqual(name, propRule.Name);
            Assert.AreEqual(message, propRule.Message);
            Assert.AreEqual("Habanero.BO", dmPropRule.Assembly);
            Assert.AreEqual(propRuleType, dmPropRule.Class);
        }

        [Test]
        public void Test_CreatePropRuleDate()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string name = TestUtilsShared.GetRandomString();
            string message = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IPropRule propRule = defClassFactory.CreatePropRuleDate(name, message);
            //---------------Test Result -----------------------
            AssertPropRuleCreatedCorrectly(name, message, propRule, "PropRuleDate");
            DMPropRule rule = (DMPropRule)propRule;
            Assert.AreEqual("min", rule.PropRuleParameters[0].KeyName);
            Assert.AreEqual(1, rule.PropRuleParameters[0].OrdinalPosition);
            Assert.AreEqual("max", rule.PropRuleParameters[1].KeyName);
            Assert.AreEqual(2, rule.PropRuleParameters[1].OrdinalPosition);
        }

        [Test]
        public void Test_CreatePropRuleDecimal()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string name = TestUtilsShared.GetRandomString();
            string message = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IPropRule propRule = defClassFactory.CreatePropRuleDecimal(name, message);
            //---------------Test Result -----------------------
            AssertPropRuleCreatedCorrectly(name, message, propRule, "PropRuleDecimal");
            DMPropRule rule = (DMPropRule) propRule;
            Assert.AreEqual("min", rule.PropRuleParameters[0].KeyName);
            Assert.AreEqual(1, rule.PropRuleParameters[0].OrdinalPosition);
            Assert.AreEqual("max", rule.PropRuleParameters[1].KeyName);
            Assert.AreEqual(2, rule.PropRuleParameters[1].OrdinalPosition);
        }

        [Test]
        public void Test_CreatePropRuleInteger()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string name = TestUtilsShared.GetRandomString();
            string message = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IPropRule propRule = defClassFactory.CreatePropRuleInteger(name, message);
            //---------------Test Result -----------------------
            AssertPropRuleCreatedCorrectly(name, message, propRule, "PropRuleInteger");
            DMPropRule rule = (DMPropRule)propRule;
            Assert.AreEqual("min", rule.PropRuleParameters[0].KeyName);
            Assert.AreEqual(1, rule.PropRuleParameters[0].OrdinalPosition);
            Assert.AreEqual("max", rule.PropRuleParameters[1].KeyName);
            Assert.AreEqual(2, rule.PropRuleParameters[1].OrdinalPosition);
        }

        [Test]
        public void Test_CreatePropRuleString()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string name = TestUtilsShared.GetRandomString();
            string message = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IPropRule propRule = defClassFactory.CreatePropRuleString(name, message);
            //---------------Test Result -----------------------
            AssertPropRuleCreatedCorrectly(name, message, propRule, "PropRuleString");
            DMPropRule rule = (DMPropRule)propRule;
            Assert.AreEqual("minLength", rule.PropRuleParameters[0].KeyName);
            Assert.AreEqual(1, rule.PropRuleParameters[0].OrdinalPosition);
            Assert.AreEqual("maxLength", rule.PropRuleParameters[1].KeyName);
            Assert.AreEqual(2, rule.PropRuleParameters[1].OrdinalPosition);
            Assert.AreEqual("patternMatch", rule.PropRuleParameters[2].KeyName);
            Assert.AreEqual(3, rule.PropRuleParameters[2].OrdinalPosition);
            Assert.AreEqual("patternMatchMessage", rule.PropRuleParameters[3].KeyName);
            Assert.AreEqual(4, rule.PropRuleParameters[3].OrdinalPosition);
        }

        [Test]
        public void Test_CreatePropRuleSingle()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string name = TestUtilsShared.GetRandomString();
            string message = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IPropRule propRule = defClassFactory.CreatePropRuleSingle(name, message);
            //---------------Test Result -----------------------
            AssertPropRuleCreatedCorrectly(name, message, propRule, "PropRuleSingle");
            DMPropRule rule = (DMPropRule)propRule;
            Assert.AreEqual("min", rule.PropRuleParameters[0].KeyName);
            Assert.AreEqual(1, rule.PropRuleParameters[0].OrdinalPosition);
            Assert.AreEqual("max", rule.PropRuleParameters[1].KeyName);
            Assert.AreEqual(2, rule.PropRuleParameters[1].OrdinalPosition);
        }

        [Test]
        public void Test_CreatePropRuleDouble()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string name = TestUtilsShared.GetRandomString();
            string message = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IPropRule propRule = defClassFactory.CreatePropRuleDouble(name, message);
            //---------------Test Result -----------------------
            AssertPropRuleCreatedCorrectly(name, message, propRule, "PropRuleDouble");
            DMPropRule rule = (DMPropRule)propRule;
            Assert.AreEqual("min", rule.PropRuleParameters[0].KeyName);
            Assert.AreEqual(1, rule.PropRuleParameters[0].OrdinalPosition);
            Assert.AreEqual("max", rule.PropRuleParameters[1].KeyName);
            Assert.AreEqual(2, rule.PropRuleParameters[1].OrdinalPosition);
        }

        [Test]
        public void Test_CreateFilterPropertyDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UITypeDefinition typeDefinition = solution.UIFilterControlTypes[0];
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string propertyName = TestUtilsShared.GetRandomString();
            string label = TestUtilsShared.GetRandomString();
            const FilterClauseOperator clauseOperator = FilterClauseOperator.OpGreaterThanOrEqualTo;
            Dictionary<string, string> parameters = new Dictionary<string, string> 
                                                        { { "1", TestUtilsShared.GetRandomString() },
                                                          { "2", TestUtilsShared.GetRandomString() }};
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IFilterPropertyDef filterPropertyDef =
                defClassFactory.CreateFilterPropertyDef(propertyName, label, typeDefinition.TypeName,
                                                        typeDefinition.AssemblyName, clauseOperator, parameters);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIGridFilterProp), filterPropertyDef);
            Assert.AreEqual(label, filterPropertyDef.Label);
            Assert.AreEqual(typeDefinition.TypeName, filterPropertyDef.FilterType);
            Assert.AreEqual(typeDefinition.AssemblyName, filterPropertyDef.FilterTypeAssembly);
            Assert.AreEqual(clauseOperator, filterPropertyDef.FilterClauseOperator);
            UIGridFilterProp gridFilterProp = (UIGridFilterProp) filterPropertyDef;
            Assert.AreEqual(parameters.Count, gridFilterProp.UIParameters.Count);
        }

        [Test]
        public void Test_CreateFilterDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            UITypeDefinition typeDefinition = solution.UIFilterControlTypes[0];
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            IFilterPropertyDef filterPropertyDef =
                defClassFactory.CreateFilterPropertyDef(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), typeDefinition.TypeName,
                                                        typeDefinition.AssemblyName, FilterClauseOperator.OpGreaterThanOrEqualTo, new Dictionary<string, string>());
            List<IFilterPropertyDef> filterPropertyDefs = new List<IFilterPropertyDef> {filterPropertyDef};
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IFilterDef filterDef = defClassFactory.CreateFilterDef(filterPropertyDefs);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIGridFilter), filterDef);
            Assert.AreEqual(1, filterDef.FilterPropertyDefs.Count);
            Assert.AreSame(filterPropertyDef, filterDef.FilterPropertyDefs[0]);
        }

        [Test]
        public void Test_CreatePropDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IPropDefCol propDefCol = defClassFactory.CreatePropDefCol();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMPropertyCollection), propDefCol);
        }

        [Test]
        public void Test_CreateKeyDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            KeyDefCol propDefCol = defClassFactory.CreateKeyDefCol();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(KeyDefCol), propDefCol);
        }

        [Test]
        public void Test_CreateRelPropDef()
        {
            //---------------Set up test pack-------------------
            //Note_: The linking of the actual properties to the RelProp is only done later by 
            //the PostLoadProcessor.
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            DMClass dmClass = new DMClass();
            dmClass.Assembly = solution.DefaultAssembly;
            DMProperty dmProperty = new DMProperty();
            dmProperty.PropertyName = TestUtilsShared.GetRandomString();
            dmProperty.Class = dmClass;
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string relPropName = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IRelPropDef propDef = defClassFactory.CreateRelPropDef(dmProperty.PropertyName, relPropName);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMRelationshipProperty), propDef);
            Assert.AreEqual(dmProperty.PropertyName, propDef.OwnerPropertyName);
            Assert.AreEqual(relPropName, propDef.RelatedClassPropName);
        }

        [Test]
        public void Test_CreateRelKeyDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IRelKeyDef relKeyDef = defClassFactory.CreateRelKeyDef();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMRelationshipPropertyCollection), relKeyDef);
        }

        [Test]
        public void Test_CreateRelationshipDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IRelationshipDefCol relationshipDefCol = defClassFactory.CreateRelationshipDefCol();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMRelationshipCollection), relationshipDefCol);
        }

        [Test]
        public void Test_CreateSingleRelationshipDef()
        {
            //Note_: The linking of the actual properties to the RelProp is only done later by 
            //the PostLoadProcessor.
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            DMRelationshipPropertyCollection relKeyDef = new DMRelationshipPropertyCollection();
            DMRelationshipProperty relationshipProperty = new DMRelationshipProperty();
            relKeyDef.Add(relationshipProperty);
            string relationshipName = TestUtilsShared.GetRandomString();
            string assemblyName = TestUtilsShared.GetRandomString();
            string relatedClassName = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------

            IRelationshipDef relationshipDef =
                defClassFactory.CreateSingleRelationshipDef(relationshipName,
                                                            assemblyName,
                                                            relatedClassName,
                                                            relKeyDef, false, DeleteParentAction.DereferenceRelated,
                                                            InsertParentAction.DoNothing, RelationshipType.Aggregation);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMRelationship), relationshipDef);
            Assert.AreEqual(relationshipName, relationshipDef.RelationshipName);
            Assert.AreEqual(assemblyName, relationshipDef.RelatedObjectAssemblyName);
            Assert.AreEqual(relatedClassName, relationshipDef.RelatedObjectClassName);
            Assert.AreEqual(relKeyDef.Count, relationshipDef.RelKeyDef.Count);
            Assert.AreSame(relKeyDef[0], ((DMRelationshipPropertyCollection)relationshipDef.RelKeyDef)[0]);
            Assert.AreEqual(DeleteParentAction.DereferenceRelated, relationshipDef.DeleteParentAction);
            Assert.AreEqual(InsertParentAction.DoNothing, relationshipDef.InsertParentAction);
            Assert.AreEqual(RelationshipType.Aggregation, relationshipDef.RelationshipType);
            Assert.AreEqual(Cardinality.Single, ((DMRelationship)relationshipDef).Cardinality);
        }

        [Test]
        public void Test_CreateMultipleRelationshipDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            DMRelationshipPropertyCollection relKeyDef = new DMRelationshipPropertyCollection();
            DMRelationshipProperty relationshipProperty = new DMRelationshipProperty();
            relKeyDef.Add(relationshipProperty);
            string relationshipName = TestUtilsShared.GetRandomString();
            string assemblyName = TestUtilsShared.GetRandomString();
            string relatedClassName = TestUtilsShared.GetRandomString();
            string orderBy = TestUtilsShared.GetRandomString();
            int timeout = TestUtilsShared.GetRandomInt();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------

            IRelationshipDef relationshipDef =
                defClassFactory.CreateMultipleRelationshipDef(relationshipName,
                                                              assemblyName,
                                                              relatedClassName,
                                                              relKeyDef, false, orderBy, 
                                                              DeleteParentAction.DereferenceRelated,
                                                              InsertParentAction.DoNothing, RelationshipType.Aggregation,
                                                              timeout);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMRelationship), relationshipDef);
            Assert.AreEqual(relationshipName, relationshipDef.RelationshipName);
            Assert.AreEqual(assemblyName, relationshipDef.RelatedObjectAssemblyName);
            Assert.AreEqual(relatedClassName, relationshipDef.RelatedObjectClassName);
            Assert.AreEqual(relKeyDef.Count, relationshipDef.RelKeyDef.Count);
            Assert.AreSame(relKeyDef[0], ((DMRelationshipPropertyCollection)relationshipDef.RelKeyDef)[0]);
            Assert.AreEqual(DeleteParentAction.DereferenceRelated, relationshipDef.DeleteParentAction);
            Assert.AreEqual(InsertParentAction.DoNothing, relationshipDef.InsertParentAction);
            Assert.AreEqual(RelationshipType.Aggregation, relationshipDef.RelationshipType);
            DMRelationship dmRelationship = ((DMRelationship)relationshipDef);
            Assert.AreEqual(orderBy,  dmRelationship.DMSortOrder.DMSortProperties[0].PropertyName);
            Assert.AreEqual(timeout, dmRelationship.Timeout);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.Cardinality);
        }

        [Test]
        public void Test_CreateSuperClassDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string assemblyName = TestUtilsShared.GetRandomString();
            string className = TestUtilsShared.GetRandomString();
            string id = TestUtilsShared.GetRandomString();
            string discriminator = TestUtilsShared.GetRandomString();
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            ISuperClassDef superClassDef = 
                defClassFactory.CreateSuperClassDef(assemblyName, className,
                                                    ORMapping.ConcreteTableInheritance, id, discriminator);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMInheritanceRelationship), superClassDef);
            Assert.AreEqual(className, superClassDef.ClassName);
            Assert.AreEqual(assemblyName, superClassDef.AssemblyName);

            Assert.AreEqual(discriminator, superClassDef.Discriminator);
            Assert.AreEqual(ORMapping.ConcreteTableInheritance, superClassDef.ORMapping);
            Assert.AreEqual(id, superClassDef.ID);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CreateUIFormColumn()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            IUIFormColumn formColumn = defClassFactory.CreateUIFormColumn();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIColumnLayout), formColumn);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CreateUIFormTab()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            IUIFormTab formTab = defClassFactory.CreateUIFormTab();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UITab), formTab);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CreateUIFormDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            IUIForm form = defClassFactory.CreateUIFormDef();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIFormInfo), form);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CreateUIGridProperty()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string heading = TestUtilsShared.GetRandomString();
            string propertyName = TestUtilsShared.GetRandomString();
            string gridControlTypeName = solution.UIGridColumnControlTypes[0].TypeName;
            string gridControlAssembly = solution.UIGridColumnControlTypes[0].AssemblyName;
            bool editable = TestUtilsShared.GetRandomBoolean();
            int width = TestUtilsShared.GetRandomInt();
            const PropAlignment alignment = PropAlignment.centre;
            Hashtable attributes = new Hashtable { { "1", "One" } };
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            IUIGridColumn uiGridColumn =
                defClassFactory.CreateUIGridProperty(heading, propertyName, gridControlTypeName, gridControlAssembly,
                                                     editable, width, alignment, attributes);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIGridColumnInfo), uiGridColumn);
            Assert.AreEqual(heading, uiGridColumn.Heading);
            Assert.AreEqual(propertyName, uiGridColumn.PropertyName);
            Assert.AreEqual(editable, uiGridColumn.Editable);
            Assert.AreEqual(width, uiGridColumn.Width);
            Assert.AreEqual(alignment, uiGridColumn.Alignment);
            Assert.AreSame(gridControlTypeName, uiGridColumn.GridControlTypeName);
            Assert.AreEqual(gridControlAssembly, uiGridColumn.GridControlAssemblyName);

            UIGridColumnInfo column = (UIGridColumnInfo)uiGridColumn;
            Assert.AreSame(solution.UIGridColumnControlTypes[0], column.UIGridColumnControlType);
            Assert.AreEqual(attributes.Count, column.UIParameters.Count);
            Assert.AreEqual(attributes["1"], column.UIParameters[0].Value);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CreateUIGridProperty_WithCustomUnknownGridColumnType()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string heading = TestUtilsShared.GetRandomString();
            string propertyName = TestUtilsShared.GetRandomString();
            string gridControlTypeName = TestUtilsShared.GetRandomString();
            string gridControlAssembly = TestUtilsShared.GetRandomString();
            bool editable = TestUtilsShared.GetRandomBoolean();
            int width = TestUtilsShared.GetRandomInt();
            const PropAlignment alignment = PropAlignment.centre;
            Hashtable attributes = new Hashtable { { "1", "One" } };
            //---------------Assert PreConditions---------------     
            Assert.IsNull(solution.GetGridColumnControlType(gridControlAssembly, gridControlTypeName));
            //---------------Execute Test ----------------------
            IUIGridColumn uiGridColumn =
                defClassFactory.CreateUIGridProperty(heading, propertyName, gridControlTypeName, gridControlAssembly,
                                                     editable, width, alignment, attributes);
            //---------------Test Result -----------------------
            Assert.IsNotNull(solution.GetGridColumnControlType(gridControlAssembly, gridControlTypeName));
            Assert.IsInstanceOf(typeof(UIGridColumnInfo), uiGridColumn);
            Assert.AreEqual(heading, uiGridColumn.Heading);
            Assert.AreEqual(propertyName, uiGridColumn.PropertyName);
            Assert.AreEqual(editable, uiGridColumn.Editable);
            Assert.AreEqual(width, uiGridColumn.Width);
            Assert.AreEqual(alignment, uiGridColumn.Alignment);
            Assert.AreSame(gridControlTypeName, uiGridColumn.GridControlTypeName);
            Assert.AreEqual(gridControlAssembly, uiGridColumn.GridControlAssemblyName);

            UIGridColumnInfo column = (UIGridColumnInfo)uiGridColumn;
            Assert.AreSame(solution.GetGridColumnControlType(gridControlAssembly, gridControlTypeName), column.UIGridColumnControlType);
            Assert.AreEqual(attributes.Count, column.UIParameters.Count);
            Assert.AreEqual(attributes["1"], column.UIParameters[0].Value);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CreateUIGridProperty_WithBlankGridColumnType_ShouldHaveNullGridColumnType()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string heading = TestUtilsShared.GetRandomString();
            string propertyName = TestUtilsShared.GetRandomString();
            string gridControlTypeName = "";
            string gridControlAssembly = "";
            bool editable = TestUtilsShared.GetRandomBoolean();
            int width = TestUtilsShared.GetRandomInt();
            const PropAlignment alignment = PropAlignment.centre;
            Hashtable attributes = new Hashtable { { "1", "One" } };
            //---------------Assert PreConditions---------------     
            Assert.IsNull(solution.GetGridColumnControlType(gridControlAssembly, gridControlTypeName));
            //---------------Execute Test ----------------------
            IUIGridColumn uiGridColumn =
                defClassFactory.CreateUIGridProperty(heading, propertyName, gridControlTypeName, gridControlAssembly,
                                                     editable, width, alignment, attributes);
            //---------------Test Result -----------------------
            Assert.IsNull(solution.GetGridColumnControlType(gridControlAssembly, gridControlTypeName));
            Assert.IsInstanceOf(typeof(UIGridColumnInfo), uiGridColumn);
            Assert.AreEqual(heading, uiGridColumn.Heading);
            Assert.AreEqual(propertyName, uiGridColumn.PropertyName);
            Assert.AreEqual(editable, uiGridColumn.Editable);
            Assert.AreEqual(width, uiGridColumn.Width);
            Assert.AreEqual(alignment, uiGridColumn.Alignment);
            Assert.IsNull(((UIGridColumnInfo)uiGridColumn).UIGridColumnControlType);
            Assert.AreSame(gridControlTypeName, uiGridColumn.GridControlTypeName);
            Assert.AreEqual(gridControlAssembly, uiGridColumn.GridControlAssemblyName);

            UIGridColumnInfo column = (UIGridColumnInfo)uiGridColumn;
            Assert.AreSame(solution.GetGridColumnControlType(gridControlAssembly, gridControlTypeName), column.UIGridColumnControlType);
            Assert.AreEqual(attributes.Count, column.UIParameters.Count);
            Assert.AreEqual(attributes["1"], column.UIParameters[0].Value);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_CreateUIGridProperty_WithBlankGridColumnAssembly_ShouldFindGridColumnTypeWithMatchingTypeName()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string heading = TestUtilsShared.GetRandomString();
            string propertyName = TestUtilsShared.GetRandomString();
            UIGridColumnControlType uiGridColumnControlType = solution.UIGridColumnControlTypes[0];
            string gridControlTypeName = uiGridColumnControlType.TypeName;
            string gridControlAssembly = string.Empty;
            bool editable = TestUtilsShared.GetRandomBoolean();
            int width = TestUtilsShared.GetRandomInt();
            const PropAlignment alignment = PropAlignment.centre;
            Hashtable attributes = new Hashtable { { "1", "One" } };
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            IUIGridColumn uiGridColumn =
                defClassFactory.CreateUIGridProperty(heading, propertyName, gridControlTypeName, gridControlAssembly,
                                                     editable, width, alignment, attributes);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIGridColumnInfo), uiGridColumn);
            Assert.AreEqual(heading, uiGridColumn.Heading);
            Assert.AreEqual(propertyName, uiGridColumn.PropertyName);
            Assert.AreEqual(editable, uiGridColumn.Editable);
            Assert.AreEqual(width, uiGridColumn.Width);
            Assert.AreEqual(alignment, uiGridColumn.Alignment);
            Assert.AreSame(gridControlTypeName, uiGridColumn.GridControlTypeName);
            Assert.AreEqual(uiGridColumnControlType.AssemblyName, uiGridColumn.GridControlAssemblyName);

            UIGridColumnInfo column = (UIGridColumnInfo)uiGridColumn;
            Assert.AreSame(uiGridColumnControlType, column.UIGridColumnControlType);
            Assert.AreEqual(attributes.Count, column.UIParameters.Count);
            Assert.AreEqual(attributes["1"], column.UIParameters[0].Value);
            //---------------Tear Down -------------------------          
        }

        [Test]
        public void Test_Create_UIGridDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IUIGrid grid = defClassFactory.CreateUIGridDef();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIGridInfo), grid);
        }

        [Test]
        public void Test_CreateUIDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            IUIForm form = defClassFactory.CreateUIFormDef();
            IUIGrid grid = defClassFactory.CreateUIGridDef();
            string name = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IUIDef uidef = defClassFactory.CreateUIDef(name, form, grid);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIView), uidef);
            Assert.AreEqual(name, uidef.Name);
            Assert.AreSame(form, uidef.UIForm);
            Assert.AreSame(grid, uidef.UIGrid);
        }

        [Test]
        public void Test_CreateUIDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            UIDefCol uiDefCol = defClassFactory.CreateUIDefCol();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(UIDefCol), uiDefCol);
        }


        [Test]
        public void Test_CreateClassDef_CorrectTypeIsCreated()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), null, null, null, null, null);

            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMClass), classDef);
        }


        [Test]
        public void Test_CreateClassDef_AssemblyExists()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string assemblyName = solution.DefaultAssembly.AssemblyName;
            string className = TestUtilsShared.GetRandomString();
            string displayName = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(assemblyName, className, displayName, null, null, null, null, null); 

            //---------------Test Result -----------------------
            Assert.AreSame(solution.DefaultAssembly, ((DMClass)classDef).Assembly);
            Assert.AreEqual(assemblyName, classDef.AssemblyName);
            Assert.AreEqual(className, classDef.ClassName);
            Assert.AreEqual(displayName, classDef.DisplayName);
        }


        [Test]
        public void Test_CreateClassDef_AssemblyDoesntExist()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            string assemblyName = TestUtilsShared.GetRandomString();
            string className = TestUtilsShared.GetRandomString();
            string displayName = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, solution.Assemblies.Count);
            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(assemblyName, className, displayName, null, null, null, null, null); 

            //---------------Test Result -----------------------
            Assert.AreEqual(assemblyName, classDef.AssemblyName);
            Assert.AreEqual(className, classDef.ClassName);
            Assert.AreEqual(displayName, classDef.DisplayName);
            Assert.AreEqual(2, solution.Assemblies.Count);
            Assert.AreNotSame(solution.DefaultAssembly.AssemblyName, classDef.AssemblyName);
        }

        [Test]
        public void Test_CreateClassDef_PropDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            IPropDefCol propDefCol = new DMPropertyCollection();
            DMProperty property = new DMProperty { PropertyName = TestUtilsShared.GetRandomString() };
            propDefCol.Add(property);          
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), null,
                               propDefCol, 
                               null, null, null); 
            //---------------Test Result -----------------------
            DMClass dmClass = (DMClass) classDef;
            Assert.AreEqual(1, dmClass.Properties.Count);
            Assert.AreSame(property, dmClass.Properties[0]);
        }

        [Test]
        public void Test_CreateClassDef_PrimaryKeyDef()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            IPropDefCol propDefCol = new DMPropertyCollection();
            DMProperty dmPropertyPrimaryKey = new DMProperty { PropertyName = TestUtilsShared.GetRandomString()};
            propDefCol.Add(dmPropertyPrimaryKey);
            DMObjectIdentity primaryKeyDef = new DMObjectIdentity();
            primaryKeyDef.Properties.Add(dmPropertyPrimaryKey);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(),
                               primaryKeyDef,
                               propDefCol, null, null, null);
            //---------------Test Result -----------------------
            DMClass dmClass = (DMClass)classDef;
            Assert.AreSame(primaryKeyDef, dmClass.ObjectIdentity);
        }

        [Test]
        public void Test_CreateClassDef_KeyDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            IPropDefCol propDefCol = new DMPropertyCollection();
            DMProperty dmPropertyUniqueKey = new DMProperty { PropertyName = TestUtilsShared.GetRandomString() };
            propDefCol.Add(dmPropertyUniqueKey);
            DMUniqueConstraint uniqueConstraint = new DMUniqueConstraint();
            uniqueConstraint.UniqueConstraintName = TestUtilsShared.GetRandomString();
            uniqueConstraint.AddPropertyInfo(dmPropertyUniqueKey);
            KeyDefCol keyDefCol = new KeyDefCol();
            keyDefCol.Add(uniqueConstraint);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), null, propDefCol,
                               keyDefCol, 
                               null, null);
            //---------------Test Result -----------------------
            DMClass dmClass = (DMClass)classDef;
            Assert.AreEqual(1, dmClass.UniqueConstraints.Count);
            Assert.AreSame(uniqueConstraint, dmClass.UniqueConstraints[0]);
        }

        [Test]
        public void Test_CreateClassDef_RelationshipDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            IPropDefCol propDefCol = new DMPropertyCollection();
            DMProperty property = new DMProperty { PropertyName = TestUtilsShared.GetRandomString() };
            propDefCol.Add(property);
            IRelationshipDef relationshipDef = new DMRelationship();
            IRelationshipDefCol relationshipDefCol = new DMRelationshipCollection();
            relationshipDefCol.Add(relationshipDef);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), null, propDefCol, null,
                               relationshipDefCol, 
                               null);
            //---------------Test Result -----------------------
            DMClass dmClass = (DMClass)classDef;
            Assert.AreEqual(relationshipDefCol.Count, dmClass.OwnerRelationships.Count);
            Assert.AreSame(relationshipDef, dmClass.OwnerRelationships[0]);
        }

        [Test]
        public void Test_CreateClassDef_UIDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            IPropDefCol propDefCol = new DMPropertyCollection();
            DMProperty property = new DMProperty { PropertyName = TestUtilsShared.GetRandomString() };
            propDefCol.Add(property);
            UIDefCol uiDefCol = new UIDefCol();
            IUIDef uiDef = new UIView();
            uiDef.Name = TestUtilsShared.GetRandomString();
            uiDefCol.Add(uiDef);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IClassDef classDef = defClassFactory.
                CreateClassDef(TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), TestUtilsShared.GetRandomString(), null, propDefCol, null, null,
                               uiDefCol);
            //---------------Test Result -----------------------
            DMClass dmClass = (DMClass)classDef;
            Assert.AreEqual(uiDefCol.Count, dmClass.UIViews.Count);
            Assert.AreSame(uiDef, dmClass.UIViews[0]);
        }

        [Test]
        public void Test_CreateClassDefCol()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateSavedDMSolution();
            IDefClassFactory defClassFactory = new FMDefClassFactory(solution);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            ClassDefCol classDefCol = defClassFactory.CreateClassDefCol();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(ClassDefCol), classDefCol);
        }

    }

}